package cn.rocksolid.sport.agency.service.impl;

import cn.rocksolid.sport.agency.dao.AgencyDao;
import cn.rocksolid.sport.agency.entity.AgencyEntity;
import cn.rocksolid.sport.agency.service.AgencyService;
import cn.rocksolid.sport.common.C;
import cn.rocksolid.sport.common.error.RSE;
import cn.rocksolid.sport.common.utils.AssertUtils;
import cn.rocksolid.sport.common.utils.QwUtils;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AgencyServiceImpl extends ServiceImpl<AgencyDao, AgencyEntity> implements AgencyService {

  @Override
  public IPage<AgencyEntity> paging(final IPage<AgencyEntity> page, final JSONObject cond) {
    return this.page(page, QwUtils.fuzzy(cond, C.TITLE.f, C.SUMMARY.f));
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public AgencyEntity create(final AgencyEntity agencyEntity) {
    AssertUtils.isNotNull(agencyEntity, RSE.INVALID_PAYLOAD);
    AssertUtils.isName(agencyEntity.getTitle(), RSE.INVALID_AGN);
    AssertUtils.isNull(this.getById(agencyEntity.getId()), RSE.DUP_AGN);
    this.save(agencyEntity);
    return agencyEntity;
  }
}
